/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.cen.disp;

import core.para.Para;
import gui.VTL1;
import gui.processor.Processor;
import gui.utils.Borders;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author jarcher
 */
public class SeqDisplay extends JPanel {
    private JScrollPane YScrollBar, XScrollBar;
    private int scrollYPos, scrollXPos;
    private SeqPane rp;
    private JPanel YScrollArea, XScrollArea;
    VTL1 vtl1;
    public SeqDisplay(VTL1 vtl1){

        this.vtl1 = vtl1;
        this.setLayout(new BorderLayout());
        rp = new SeqPane(vtl1);
        rp.setBounds(0, 0, vtl1.getWidth()-4, vtl1.getHeight());
        this.add(rp, BorderLayout.CENTER);
        
        YScrollArea = new JPanel();
        YScrollArea.setPreferredSize(new Dimension(1, Borders.SEQCHARHEIGHT * Processor.tScripts.seqs.size()));
        YScrollBar = new JScrollPane(YScrollArea);
        YScrollBar.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        YScrollBar.getViewport().setBackground(Color.white);
        YScrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        YScrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        AdjustmentListener YScrollListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent){
                scrollYPos = YScrollBar.getVerticalScrollBar().getModel().getValue();
                Para.START_VERTICAL_VIEW = scrollYPos / Borders.SEQCHARHEIGHT;
                Para.END_VERTICAL_VIEW = Para.START_VERTICAL_VIEW + (int)(getSize().height/Borders.SEQCHARHEIGHT) + 1;
                rp.updateReads();
            }
        };
        YScrollBar.getVerticalScrollBar().addAdjustmentListener(YScrollListener);
        this.add(YScrollBar, BorderLayout.EAST);
        
        
        XScrollArea = new JPanel();
        XScrollArea.setPreferredSize(new Dimension((Borders.SEQ_CHAR_WIDTH * Processor.LONGEST_TRANSCRIPT) + SeqPane.TITLE_SPACE, 1));
        XScrollBar = new JScrollPane(XScrollArea);
        XScrollBar.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        XScrollBar.getViewport().setBackground(Color.white);
        XScrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        XScrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        AdjustmentListener XScrollListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent){
                scrollXPos = XScrollBar.getHorizontalScrollBar().getModel().getValue();
                Para.START_HORIZONTAL_VIEW = scrollXPos / Borders.SEQ_CHAR_WIDTH;
                Para.END_HORIZONTAL_VIEW = Para.START_HORIZONTAL_VIEW + (int)(getSize().width/Borders.SEQ_CHAR_WIDTH) + 1;
                rp.updateReads();
            }
        };
        XScrollBar.getHorizontalScrollBar().addAdjustmentListener(XScrollListener);
        this.add(XScrollBar, BorderLayout.SOUTH);
        
        
        this.addComponentListener(new ComponentAdapter(){
            public void componentResized(ComponentEvent e){rp.updateReads();}
        }); 
    }
}
